﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Infragistics.Win.UltraWinDataSource;
using ATA.Data.MisaDAL;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;

using ATA.Entities;
using ATA.Business;

namespace ATA.Winforms
{
    public partial class frmDangkyLapmayPhamau : Form
    {

        private Guid gv_khachHangDuThuongID;
        KhachHangLapMayPhaMau gv_entLapMay;
        TList<DoanhSoCamKetLapMayPhaMau> gv_lsDoanhSoCamKet ;

        public frmDangkyLapmayPhamau(string pv_strKhachHangDangKyID)
        {
            gv_khachHangDuThuongID = new Guid(pv_strKhachHangDangKyID);

            KhachHangLapMayPhaMauService v_srvDuthuong = new KhachHangLapMayPhaMauService();
            gv_entLapMay = v_srvDuthuong.GetById(gv_khachHangDuThuongID);

            InitializeComponent();

            btnCapNhat.Visible = true;
            btnDelete.Visible = true;
            btnThem.Enabled = false;
        }

        public frmDangkyLapmayPhamau()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmDangkyLapmayPhamau_Load(object sender, EventArgs e)
        {
            LoadCustomerList();
            LoadBrandList();
            //LoadKhachHangDangKyLapMay();
            LoadDoanhSoCamKet(36);

            if (gv_entLapMay != null)
            {
                txtSobienban.Value = gv_entLapMay.SoBienBan;
                dtmNgayKy.Value = gv_entLapMay.NgayKyBienBan;
                // khach hang
                VwAllCustomersService v_srvAllCustomer = new VwAllCustomersService();
                VList<VwAllCustomers> v_lsCustomers = v_srvAllCustomer.Get("KhachHangID = '" + gv_entLapMay.KhachHangId.ToString() + "'", "");
                txtCustomers.Value = v_lsCustomers[0].MaKh;
                txtCustomerName.Value = v_lsCustomers[0].TenKh;
                txtCustomers.Enabled = false;

                dtmStartDate.Value = gv_entLapMay.ThoiDiemLapMay;
                rdoLoaiMay.Value = gv_entLapMay.LoaiMay;
                txtDoanhSoToiThieu.Value = gv_entLapMay.DoanhSoToiThieu;
                txtGhiChu.Value = gv_entLapMay.GhiChu;
            }
        }

        private void LoadKhachHangDangKyLapMay()
        {
            VwKhachHangLapMayPhaMauService v_srvLapMay = new VwKhachHangLapMayPhaMauService();
            VList<VwKhachHangLapMayPhaMau> v_lsKhachHangLapMay = v_srvLapMay.GetAll();

            grdDanhsachDangky.DataSource = v_lsKhachHangLapMay;
            grdDanhsachDangky.DataBind();

            grdDanhsachDangky.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            grdDanhsachDangky.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
            grdDanhsachDangky.DisplayLayout.Bands[0].Columns["KhachHangID"].Hidden = true;
            grdDanhsachDangky.DisplayLayout.Bands[0].Columns["LoaiMay"].Hidden = true;
        }

        private void LoadBrandList()
        {
            ThuongHieuService v_srvThuongHieu = new ThuongHieuService();
            TList<ThuongHieu> v_lsThuongHieu = v_srvThuongHieu.Find("IsActive = TRUE");

            cboBrandNames.ValueMember = "BrandID";
            cboBrandNames.DisplayMember = "BrandName";
            cboBrandNames.DataSource = v_lsThuongHieu;
            cboBrandNames.DataBind();
        }
        private void LoadCustomerList()
        {
            foreach (AccountingObject v_lsParentCusomers in Globals.gv_lsCustomers)
            {
                UltraDataRow row = dsCustomers.Rows.Add();
                row["AccountingObjectId"] = v_lsParentCusomers.AccountingObjectId.ToString();
                row["ParentAccountingObjectId"] = v_lsParentCusomers.ParentAccountingObjectId.ToString();
                row["AccountingObjectName"] = v_lsParentCusomers.AccountingObjectName;
                row["AccountingObjectCode"] = v_lsParentCusomers.AccountingObjectCode;
                foreach (AccountingObject v_lsChildCusomers in Globals.gv_lsCustomersNotInMisa)
                {
                    if (v_lsChildCusomers.ParentAccountingObjectId.Equals(v_lsParentCusomers.AccountingObjectId))
                    {
                        UltraDataRowsCollection childRows = row.GetChildRows("Band 1");
                        UltraDataRow childRow = childRows.Add();
                        childRow["ParentAccountingObjectId"] = v_lsChildCusomers.ParentAccountingObjectId.ToString();
                        childRow["AccountingObjectId"] = v_lsChildCusomers.AccountingObjectId.ToString();
                        childRow["AccountingObjectName"] = v_lsChildCusomers.AccountingObjectName;
                        childRow["AccountingObjectCode"] = v_lsChildCusomers.AccountingObjectCode;
                    }
                }

            }

            grdCustomers.DataSource = dsCustomers;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.Width = 520;
            grdCustomers.DataBind();

            DropDownEditorButton objKhachHang = new DropDownEditorButton();
            objKhachHang.Control = this.grdCustomers;
            txtCustomers.ButtonsRight.Add(objKhachHang);
        }

        private void grdCustomers_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            txtCustomers.Focus();
        }

        private void txtCustomers_AfterEditorButtonCloseUp(object sender, EditorButtonEventArgs e)
        {
            if (grdCustomers.ActiveRow.Index > -1)
            {
                if (grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value != null)
                {
                    txtCustomers.Text = grdCustomers.ActiveRow.Cells["AccountingObjectCode"].Value.ToString();
                    txtCustomerName.Text = grdCustomers.ActiveRow.Cells["AccountingObjectName"].Value.ToString();
                }
            }
        }

        private void LoadDataByCustomer(Guid pv_guidCustomerID)
        {
            //KhachHangDuthuongService v_srvKhachHangDuThuong = new KhachHangDuthuongService();
            //KhachHangDuthuong v_entKhachHangDuThuong = v_srvKhachHangDuThuong.GetByCustomerID(pv_guidCustomerID);

            //if (v_entKhachHangDuThuong != null)
            //{
            //    rdoLoaiThoigian.Value = v_entKhachHangDuThuong.Duration;
            //    dtmStartDate.Value = v_entKhachHangDuThuong.StartDate;
            //    dtmEndDate.Value = v_entKhachHangDuThuong.EndDate;
            //    cboBrandNames.Value = v_entKhachHangDuThuong.BrandId;

            //    DoanhSoCamKetLapMayPhaMauService v_srvDoanhSoCamKet = new DoanhSoCamKetLapMayPhaMauService();
            //    TList<DoanhSoCamKetLapMayPhaMau> v_lsDoanhSoCamKet = v_srvDoanhSoCamKet.GetByCustomerID(v_entKhachHangDuThuong.Id);

            //    grdDanhsachDangky.DataSource = v_lsDoanhSoCamKet;
            //    grdDanhsachDangky.DataBind();

            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["Doanhso"].CellActivation = Activation.AllowEdit;
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["Doanhso"].Header.Caption = "Doanh số";
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["Year"].Header.Caption = "Năm";
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["Quarter"].Header.Caption = "Quý";
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["Month"].Header.Caption = "Tháng";
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["KhachHangDuThuongID"].Hidden = true;
            //    grdDanhsachDangky.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = true;
            //}
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangLapMayPhaMauService v_srvLapMay = new KhachHangLapMayPhaMauService();
                
                gv_entLapMay.SoBienBan = txtSobienban.Text;
                gv_entLapMay.NgayKyBienBan = dtmNgayKy.DateTime;

                //gv_entLapMay.KhachHangId = new Guid(grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value.ToString());
                gv_entLapMay.LoaiMay = Convert.ToInt16(rdoLoaiMay.Value.ToString());
                gv_entLapMay.DoanhSoToiThieu = Convert.ToDecimal(txtDoanhSoToiThieu.Value);
                gv_entLapMay.IsActive = true;
                gv_entLapMay.ThoiDiemLapMay = new DateTime(dtmStartDate.DateTime.Year, dtmStartDate.DateTime.Month, 1);
                gv_entLapMay.GhiChu = txtGhiChu.Text;
                gv_entLapMay.UpdateDate = DateTime.Now;

                v_srvLapMay.Save(gv_entLapMay);

                DoanhSoCamKetLapMayPhaMauService v_srvDoanhSoCamKet = new DoanhSoCamKetLapMayPhaMauService();
                
                foreach (UltraGridRow v_rDoanhso in grdDoanhsoCamket.Rows)
                {
                    DoanhSoCamKetLapMayPhaMau v_entDoanhso = v_srvDoanhSoCamKet.GetById(Convert.ToInt16(v_rDoanhso.Cells["ID"].Value));
                    //v_entDoanhso.KhachHangDuThuongId = gv_entLapMay.Id;
                    v_entDoanhso.Year = Convert.ToInt16(v_rDoanhso.Cells["Year"].Value);
                    v_entDoanhso.Quarter = Convert.ToInt16(v_rDoanhso.Cells["Quarter"].Value);
                    v_entDoanhso.Month = Convert.ToInt16(v_rDoanhso.Cells["Month"].Value);
                    v_entDoanhso.DoanhSoBase = Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoBase"].Value);
                    v_entDoanhso.DoanhSoBotBa= Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoBotBa"].Value);
                    v_entDoanhso.DoanhSoSon= Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoSon"].Value);
                    v_entDoanhso.UpdatedDate = DateTime.Now;
                    v_entDoanhso.UpdatedBy = Globals.gv_strUsername;
                    v_entDoanhso.IsActive = true;

                    v_srvDoanhSoCamKet.Save(v_entDoanhso);
                }

                MessageBox.Show(this, "Cập nhật thông tin thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //LoadKhachHangDangKyLapMay();
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Không cập nhật được thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grdDanhsachDangky_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
        {
            try
            {
                dtmEndDate.Value = e.Row.Cells["ThoiDiemLapMay"].Value;
                rdoLoaiMay.Value = e.Row.Cells["LoaiMay"].Value;
                txtDoanhSoToiThieu.Value = e.Row.Cells["DoanhSoToiThieu"].Value;
                txtCustomers.Value = e.Row.Cells["AccountingObjectID"].Value;
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, ex.Message, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void rdoLoaiThoigian_ValueChanged(object sender, EventArgs e)
        {
            LoadDoanhSoCamKet(Convert.ToInt16(rdoLoaiThoigian.Value));
        }

        private void LoadDoanhSoCamKet(int pv_iDuration)
        {
            if (gv_entLapMay == null)
            {
                switch (pv_iDuration)
                {
                    case 1:     // 1 thang
                        dsDoanhso.Rows.Clear();

                        for (int i = 0; i < 1; i++)
                        {
                            UltraDataRow v_grDoanhso = dsDoanhso.Rows.Add();
                            v_grDoanhso["Year"] = (int)(i / 12) + 1;
                            v_grDoanhso["Quarter"] = (int)(i / 3) + 1;
                            v_grDoanhso["Month"] = i + 1;
                        }
                        grdDoanhsoCamket.DataSource = dsDoanhso;
                        grdDoanhsoCamket.DataBind();
                        break;
                    case 3:     // 1 quy
                        dsDoanhso.Rows.Clear();

                        for (int i = 0; i < 3; i++)
                        {
                            UltraDataRow v_grDoanhso = dsDoanhso.Rows.Add();
                            v_grDoanhso["Year"] = (int)(i / 12) + 1;
                            v_grDoanhso["Quarter"] = (int)(i / 3) + 1;
                            v_grDoanhso["Month"] = i + 1;
                        }
                        grdDoanhsoCamket.DataSource = dsDoanhso;
                        grdDoanhsoCamket.DataBind();
                        break;
                    case 12:     // 1 nam
                        dsDoanhso.Rows.Clear();

                        for (int i = 0; i < 12; i++)
                        {
                            UltraDataRow v_grDoanhso = dsDoanhso.Rows.Add();
                            v_grDoanhso["Year"] = (int)(i / 12) + 1;
                            v_grDoanhso["Quarter"] = (int)(i / 3) + 1;
                            v_grDoanhso["Month"] = i + 1;
                        }
                        grdDoanhsoCamket.DataSource = dsDoanhso;
                        grdDoanhsoCamket.DataBind();
                        break;
                    case 36:     // 3 nam
                        dsDoanhso.Rows.Clear();
                        for (int i = 0; i < 36; i++)
                        {
                            UltraDataRow v_grDoanhso = dsDoanhso.Rows.Add();
                            v_grDoanhso["Year"] = (int)(i / 12) + 1;
                            v_grDoanhso["Quarter"] = (int)(i / 3) + 1;
                            v_grDoanhso["Month"] = i + 1;
                        }
                        grdDoanhsoCamket.DataSource = dsDoanhso;
                        grdDoanhsoCamket.DataBind();
                        break;
                    default:
                        break;
                }
            }
            else
            {
                DoanhSoCamKetLapMayPhaMauService v_srvDoanhSoCamKet = new DoanhSoCamKetLapMayPhaMauService();
                gv_lsDoanhSoCamKet = v_srvDoanhSoCamKet.Find("KhachHangDuThuongID = '" + gv_khachHangDuThuongID.ToString() + "'");
                grdDoanhsoCamket.DataSource = gv_lsDoanhSoCamKet;
                grdDoanhsoCamket.DataBind();

                grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
                grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["KhachHangDuThuongID"].Hidden= true;
                grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = true;
                grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["UpdatedDate"].Hidden = true;
                grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["UpdatedBy"].Hidden = true;
            }
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoBase"].CellActivation = Activation.AllowEdit;
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoBase"].Format = "###,###,##0";
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoBotBa"].CellActivation = Activation.AllowEdit;
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoBotBa"].Format = "###,###,##0";
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoSon"].CellActivation = Activation.AllowEdit;
            grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["DoanhSoSon"].Format = "###,###,##0";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangLapMayPhaMauService v_srvLapMay = new KhachHangLapMayPhaMauService();
                KhachHangLapMayPhaMau v_entLapMay = new KhachHangLapMayPhaMau();

                v_entLapMay.Id = Guid.NewGuid();

                v_entLapMay.SoBienBan = txtSobienban.Text;
                v_entLapMay.NgayKyBienBan = dtmNgayKy.DateTime;

                v_entLapMay.KhachHangId = new Guid(grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value.ToString());
                v_entLapMay.LoaiMay = Convert.ToInt16(rdoLoaiMay.Value.ToString());
                v_entLapMay.DoanhSoToiThieu = Convert.ToDecimal(txtDoanhSoToiThieu.Value);
                v_entLapMay.IsActive = true;
                v_entLapMay.ThoiDiemLapMay = new DateTime(dtmStartDate.DateTime.Year, dtmStartDate.DateTime.Month, 1);
                v_entLapMay.GhiChu = txtGhiChu.Text;
                v_entLapMay.UpdateDate = DateTime.Now;

                v_srvLapMay.Save(v_entLapMay);

                DoanhSoCamKetLapMayPhaMauService v_srvDoanhSoCamKet = new DoanhSoCamKetLapMayPhaMauService();

                foreach (UltraGridRow v_rDoanhso in grdDoanhsoCamket.Rows)
                {
                    DoanhSoCamKetLapMayPhaMau v_entDoanhso = new DoanhSoCamKetLapMayPhaMau();
                    v_entDoanhso.KhachHangDuThuongId = v_entLapMay.Id;
                    v_entDoanhso.Year = Convert.ToInt16(v_rDoanhso.Cells["Year"].Value);
                    v_entDoanhso.Quarter = Convert.ToInt16(v_rDoanhso.Cells["Quarter"].Value);
                    v_entDoanhso.Month = Convert.ToInt16(v_rDoanhso.Cells["Month"].Value);
                    v_entDoanhso.DoanhSoBase = Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoBase"].Value);
                    v_entDoanhso.DoanhSoBotBa = Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoBotBa"].Value);
                    v_entDoanhso.DoanhSoSon = Convert.ToDecimal(v_rDoanhso.Cells["DoanhSoSon"].Value);
                    v_entDoanhso.UpdatedDate = DateTime.Now;
                    v_entDoanhso.UpdatedBy = Globals.gv_strUsername;
                    v_entDoanhso.IsActive = true;

                    v_srvDoanhSoCamKet.Save(v_entDoanhso);
                }

                MessageBox.Show(this, "Cập nhật thông tin thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //LoadKhachHangDangKyLapMay();
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Không cập nhật được thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangLapMayPhaMauService v_srvLapMay = new KhachHangLapMayPhaMauService();

                v_srvLapMay.Delete(gv_entLapMay);

                DoanhSoCamKetLapMayPhaMauService v_srvDoanhSoCamKet = new DoanhSoCamKetLapMayPhaMauService();

                v_srvDoanhSoCamKet.Delete(gv_lsDoanhSoCamKet);
                
                MessageBox.Show(this, "Cập nhật thông tin thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Không cập nhật được thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }




    }
}
